package com.gxb.codebook.database;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import com.gxb.codebook.bean.NotepadBean;
import com.gxb.codebook.utils.DBUtils;

public class SQLiteHelper extends SQLiteOpenHelper {
    private SQLiteDatabase sqLiteDatabase;

    // 创建数据库
    public SQLiteHelper(Context context) {
        super(context, DBUtils.DATABASE_NAME, null, DBUtils.DATABASE_VERSION);
        sqLiteDatabase = this.getWritableDatabase();
    }

    // 创建表
    @Override
    public void onCreate(SQLiteDatabase db) {
        // 创建 Note 表
        db.execSQL("CREATE TABLE " + DBUtils.NOTE_TABLE + "(" +
                DBUtils.NOTE_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
                DBUtils.NOTE_CONTENT + " TEXT, " +
                DBUtils.NOTE_TIME + " TEXT)");

        // 创建 Passwd_Note 表
        db.execSQL("CREATE TABLE " + DBUtils.PASSWD_NOTE_TABLE + "(" +
                DBUtils.PASSWD_NOTE_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
                DBUtils.PASSWD_NOTE_USER + " TEXT, " +
                DBUtils.PASSWD_NOTE_NAME + " TEXT, " +
                DBUtils.PASSWD_NOTE_PASSWD + " TEXT, " +
                DBUtils.PASSWD_NOTE_TEXT + " TEXT, " +
                DBUtils.PASSWD_NOTE_TIME + " TEXT)");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // 如果数据库版本升级，删除旧表并重新创建
        db.execSQL("DROP TABLE IF EXISTS " + DBUtils.NOTE_TABLE);
        db.execSQL("DROP TABLE IF EXISTS " + DBUtils.PASSWD_NOTE_TABLE);
        onCreate(db);
    }

    // 添加数据
    public boolean insertData(String userContent, String userTime) {
        ContentValues contentValues = new ContentValues();
        contentValues.put(DBUtils.NOTE_CONTENT, userContent);
        contentValues.put(DBUtils.NOTE_TIME, userTime);
        return sqLiteDatabase.insert(DBUtils.NOTE_TABLE, null, contentValues) > 0;
    }

    // 修改数据
    public boolean updateData(String id, String content, String userYear) {
        ContentValues contentValues = new ContentValues();
        contentValues.put(DBUtils.NOTE_CONTENT, content);
        contentValues.put(DBUtils.NOTE_TIME, userYear);
        String sql = DBUtils.NOTE_ID + "=?";
        String[] strings = new String[]{id};
        return sqLiteDatabase.update(DBUtils.NOTE_TABLE, contentValues, sql, strings) > 0;
    }

    public List<NotepadBean> getAllNotepads() {
        List<NotepadBean> notepads = new ArrayList<>();
        Cursor cursor = sqLiteDatabase.query(DBUtils.NOTE_TABLE, null, null, null, null, null, null);
        if (cursor != null) {
            while (cursor.moveToNext()) {
                int idIndex = cursor.getColumnIndex(DBUtils.NOTE_ID);
                int contentIndex = cursor.getColumnIndex(DBUtils.NOTE_CONTENT);
                int timeIndex = cursor.getColumnIndex(DBUtils.NOTE_TIME);
                String id = String.valueOf(cursor.getInt(idIndex));
                String content = cursor.getString(contentIndex);
                String time = cursor.getString(timeIndex);
                NotepadBean noteInfo = new NotepadBean();
                noteInfo.setId(id);
                noteInfo.setNotepadContent(content);
                noteInfo.setNotepadTime(time);
                notepads.add(noteInfo);
            }
        }
        return notepads;
    }
    public List<NotepadBean> getAllPasswdNotepads() {
        List<NotepadBean> notepads = new ArrayList<>();
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor cursor = db.query(DBUtils.PASSWD_NOTE_TABLE, null, null, null, null, null, null);
        if (cursor != null) {
            while (cursor.moveToNext()) {
                int idIndex = cursor.getColumnIndex(DBUtils.PASSWD_NOTE_ID);
                int userIndex = cursor.getColumnIndex(DBUtils.PASSWD_NOTE_USER);
                int nameIndex = cursor.getColumnIndex(DBUtils.PASSWD_NOTE_NAME);
                int passwdIndex = cursor.getColumnIndex(DBUtils.PASSWD_NOTE_PASSWD);
                int textIndex = cursor.getColumnIndex(DBUtils.PASSWD_NOTE_TEXT);
                int timeIndex = cursor.getColumnIndex(DBUtils.PASSWD_NOTE_TIME);
                String id = String.valueOf(cursor.getInt(idIndex));
                String user = cursor.getString(userIndex);
                String name = cursor.getString(nameIndex);
                String passwd = cursor.getString(passwdIndex);
                String text = cursor.getString(textIndex);
                String time = cursor.getString(timeIndex);
                NotepadBean noteInfo = new NotepadBean();
                noteInfo.setId(id);
                noteInfo.setNotepadUser(user);
                noteInfo.setNotepadName(name);
                noteInfo.setNotepadPasswd(passwd);
                noteInfo.setNotepadText(text);
                noteInfo.setNotepadTime(time);
                notepads.add(noteInfo);
            }
        }
        return notepads;
    }


    public void deleteNotepad(String id) {
        sqLiteDatabase.delete(DBUtils.NOTE_TABLE, DBUtils.NOTE_ID + "=?", new String[]{id});
    }
    public void deletePasswdNotepad(String id) {
        sqLiteDatabase.delete(DBUtils.PASSWD_NOTE_TABLE, DBUtils.PASSWD_NOTE_ID + "=?", new String[]{id});

    }

    public static String getTime() {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年MM月dd日 HH:mm");
        Date date = new Date(System.currentTimeMillis());
        return simpleDateFormat.format(date);
    }

    public void updateNotepad(int id, String user, String name, String passwd, String text) {
        ContentValues values = new ContentValues();
        values.put(DBUtils.PASSWD_NOTE_USER,user);
        values.put(DBUtils.PASSWD_NOTE_NAME,  name);
        values.put(DBUtils.PASSWD_NOTE_PASSWD, passwd);
        values.put(DBUtils.PASSWD_NOTE_TEXT, text);
        values.put(DBUtils.PASSWD_NOTE_TIME, getTime());
        sqLiteDatabase.update(DBUtils.PASSWD_NOTE_TABLE, values, DBUtils.PASSWD_NOTE_ID + "=?", new String[]{String.valueOf(id)});
    }

    public void insertNotepad(String user, String name, String passwd, String text) {
        ContentValues values = new ContentValues();
        values.put(DBUtils.PASSWD_NOTE_USER,user);
        values.put(DBUtils.PASSWD_NOTE_NAME,  name);
        values.put(DBUtils.PASSWD_NOTE_PASSWD, passwd);
        values.put(DBUtils.PASSWD_NOTE_TEXT, text);
        values.put(DBUtils.PASSWD_NOTE_TIME, getTime());
        sqLiteDatabase.insert(DBUtils.PASSWD_NOTE_TABLE, null, values);

    }

    public long getLastId() {
        return sqLiteDatabase.insert(DBUtils.NOTE_TABLE, null, new ContentValues()) > 0 ? sqLiteDatabase.insert(DBUtils.NOTE_TABLE, null, new ContentValues()) : -1;
    }




    //
}
